package address;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;
import java.io.*;

public class Main {
	public static void main(String[] args) {
		Connection conn = null;

		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection(
							"jdbc:mysql://localhost:3306/jmk?useUnicode=true&characterEncoding=utf-8",
							"root", "websys");
			System.out.println("db연동이 완료되었습니다.");

			Statement stmt = conn.createStatement();
			Scanner input = new Scanner(System.in);

			String id = "";
			String pw = "";

			System.out.println("AddressBook에 접속합니다. ");

			boolean menucheck = true;
			boolean pwcheck = true;
			
			while (pwcheck) {

				System.out.print("비밀번호를 입력하세요 : ");
				pw = input.next();
				boolean login_result = login(pw, id);

				if (login_result) {
					
					pwcheck = false;
					System.out.println("로그인이 완료되었습니다.");
					String ans = "";
					menucheck = true;
					
					while (menucheck) {
						menucheck = false;
						try {

							System.out.println("1. password변경");
							System.out.println("2. 전체연락처 보기");
							System.out.println("3. 연락처 추가");
							System.out.println("4. 연락처 검색");
							System.out.println("5. 연락처 전체 삭제");
							System.out.println("6. 로그아웃");

							System.out.println("항목을 선택해주세요: ");
							ans = input.next();

						} catch (InputMismatchException e) {
							System.out.println("1~6사이의 값을 입력하세요.");
							menucheck = true;
						}

						switch (ans) {
							case "1": {
								PwEdit.edit();
								menucheck = true;
								break;
							}
							case "2": {
								AddrList.showList();
								menucheck = true;
								break;
							}
							case "3": {
								AddAddr.add();
								menucheck = true;
								break;
							}
							case "4": {
								SearchAddr.search();
								menucheck = true;
								break;
							}
							case "5": {
								AddrList.deleteAll();
								menucheck = true;
								break;
							}
							case "6": {
								pwcheck = true;
								menucheck = false;// 비밀번호 입력 화면으로 넘어가기.
								System.out.println("로그아웃이 완료되었습니다.");
								break;
							}
							default: {
								System.out.println("1~6 내의 항목을 선택해주세요!");
								menucheck = true;
								break;
							}
						}
					}

				} else {
					pwcheck = true;
					System.out.println("비밀번호가 일치하지 않습니다.");
				}

			}
			input.close();

		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static boolean login(String pw, String id) {

		Connection conn = null;

		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection(
							"jdbc:mysql://localhost:3306/jmk?useUnicode=true&characterEncoding=utf-8",
							"root", "websys");
			
			System.out.println("db연동이 완료되었습니다.");

			Statement stmt = conn.createStatement();
			Scanner input = new Scanner(System.in);

			ResultSet rs = stmt.executeQuery("SELECT * FROM user where id ='user'");
			rs.next();
			
			String input_pw = rs.getString("pw");
			
			if (pw.equals(input_pw)) {
				return true;
			} else
				return false;

		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return false;
	}

}
